package com.focus.xyz.core.pages;

import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.utils.Constants;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.List;

/**
 * Created by chenmeng on 2016/4/15.
 */
public abstract class BaseTable implements ITable{
    protected Action action;
    protected WebElement table;

    protected BaseTable(Action action){
        this.action = action;
    }

    protected abstract By getLinkLocator() throws Exception;
    protected abstract By getLocator(int index) throws Exception;

    /*
    * 选中一行
    * */
    public void selectRow(WebElement table,String key,String target) throws Exception{
        selectRowAndClickLink(table,key,target);
    }

    /*
    * 针对支持多选的表格，选择多行
    * */
    public void selectRows(WebElement table,String[] keys,By selectXpath) throws Exception{
        List<WebElement> rows = table.findElements(By.tagName(Constants.TAG_TR));
        for(WebElement row:rows){
            List<WebElement> cols = row.findElements(By.tagName(Constants.TAG_TD));
            for(WebElement col:cols){
                for(String key : keys){
                    if(!key.equals(col.getText())){
                     continue;
                    }
                    WebElement select = row.findElement(selectXpath);
                    action.selectCheckBox(select);
                }
            }
        }

    }

    /*
    * 根据表格、单元格名称、要点击的链接名称 点击该链接,如果没有链接 直接点击该行
    * */
    protected void selectRowAndClickLink(WebElement table,String key,String target) throws Exception{
        List<WebElement> rows = table.findElements(By.tagName(Constants.TAG_TR));
        OUT:for(WebElement row:rows){
            List<WebElement> cols = row.findElements(By.tagName(Constants.TAG_TD));
            for(WebElement col:cols){
                if(key.equals(col.getText())){
                    if(null != getLinkLocator()){
                        List<WebElement> linkList = row.findElements(getLinkLocator());
                        for(WebElement a:linkList){
                            if(target.equals(a.getText())){
                                action.click(a);
                                break OUT;
                            }
                        }
                    }else{
                        action.click(row);
                        break OUT;

                    }
                }
            }
        }
    }

    /*
    * 根据单元格索引获取表格单元内容
    * */
    public String getCellText(WebElement table,String key,int index) throws Exception{
        String target = null;
        List<WebElement> rows = table.findElements(By.tagName(Constants.TAG_TR));
        OUT:for(WebElement row:rows){
            List<WebElement> cols = row.findElements(By.tagName(Constants.TAG_TD));
            for(WebElement col:cols){
                if(key.equals(col.getText())){
                    if(null != getLocator(index)){
                        target = row.findElement(getLocator(index)).getText();
                        break OUT;
                    }
                }
            }
        }
        return target;
    }

    public int getRowCount() throws Exception{
        return table.findElements(By.tagName(Constants.TAG_TR)).size();
    }

    public void openLink(String key,String link) throws Exception{
        selectRowAndClickLink(table,key,link);
    }

    public void linkText(String key) throws Exception{
        List<WebElement> rows = table.findElements(By.tagName(Constants.TAG_TR));
        OUT:for(WebElement row:rows){
            List<WebElement> cols = row.findElements(By.tagName(Constants.TAG_TD));
            for(WebElement col:cols){
                if(key.equals(col.getText())){
                    WebElement a = col.findElement(By.tagName(Constants.TAG_A));
                    action.click(a);
                    break OUT;

                }
            }
        }
    }
}
